﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Dune2013
{
    public class MapFactory
    {
        static MapFactory instance;
        private MapFactory() { }
        public static MapFactory getInstance() {
            if (instance == null)
                instance = new MapFactory();
            return instance;
        }
        public Map createMap( int nodesX, int nodesY, int tamNodesX, int tamNodesY, int wellAmount, int minOil, int maxOil, int obsAmount) {
            Node[,] nodes = new Node[nodesX, nodesY];
            for (int i = 0; i < nodesX; i++)
            {
                for (int j = 0; j < nodesY; j++)
                {
                    nodes[i, j] = new Node(i, j, StaticStuff.nodeScale);
                    nodes[i, j].type = Node.NODE_TYPE.NOTHING;
                    nodes[i, j].whichList = Node.LISTS.NONE;
                }
            }

            NodeManager nodeManager = NodeManager.getInstance();

            nodeManager.makeMainBase(nodes[StaticStuff.baseX,StaticStuff.baseY]);

            nodeManager.makeStation(nodes[StaticStuff.stationX,StaticStuff.stationY]);

            Random rnd = new Random();
            for (int i = 0; i < wellAmount; i++) {
                int x = rnd.Next(1, nodesX - 1) , y = rnd.Next(1, nodesY - 1);
                //Para asegurarme que no este ocupado ese nodo
                while(true){
                    if(nodes[x,y].type == Node.NODE_TYPE.NOTHING && !nodes[x,y].obstacle)
                        break;
                    x = rnd.Next(1, nodesX - 1);
                    y = rnd.Next(1, nodesY - 1);
                }
                //Cada 3 pozos hacer uno vacio. Sino hacer que tenga entre el minimo y el maximo del maxOil
                int oil = (i + 1) % 3 == 0 ? 0 : rnd.Next(minOil, maxOil);
                nodeManager.makePOI(nodes[x,y], oil);
            }

            for (int i = 0; i < obsAmount; i++ )
            {
                int x = rnd.Next(1, nodesX - 1), y = rnd.Next(1, nodesY - 1);
                //Para asegurarme que no este ocupado ese nodo
                while (true)
                {
                    if (nodes[x, y].type == Node.NODE_TYPE.NOTHING && !nodes[x, y].obstacle)
                        break;
                    x = rnd.Next(1, nodesX - 1);
                    y = rnd.Next(1, nodesY - 1);
                }
                nodeManager.makeObs(nodes[x,y]);
            }

            Map newMap = new Map(nodes, nodesX, nodesY, tamNodesX, tamNodesY);
            return newMap;
        }
    }
}
